using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using DcsGauges.Gui.Rendering;
using DcsGauges.Gui.Gauges.Concrete;
using DcsGauges.DataStructures;
using DcsGauges.Data;
using DcsGauges.Gui.Views.Concrete;
using DcsGauges.Shared.DataStructures;
using DcsGauges.Config;
using DcsGauges.Network;
using DcsGauges.Input.Mouse;
using DcsGauges.Gui;

namespace DcsGauges
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class DcsGaugesGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private ViewSwitcher            viewSwitcher                = null;


        private DataConnectorContainer  dataConnectors              = new DataConnectorContainer();
        private IDcsA10DataModel        dataModel                   = new TestingDataModel();
        private MouseEventDispatcher    mouseEventDispatcher        = new MouseEventDispatcher();

        public DcsGaugesGame()
        {
            graphics                            = new GraphicsDeviceManager(this);
            Content.RootDirectory               = "Content";
            graphics.PreferredBackBufferWidth   = 300;
            this.Window.AllowUserResizing       = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            TextureManager.ContentManager   = this.Content;
            FontManager.ContentManager      = this.Content;
            ModelManager.ContentManager     = this.Content;
            this.IsMouseVisible             = true;

            if(ClientConfigSection.Loaded.AutoConnect == true)
            {
                ServerChannel channel       = new ServerChannel(ClientConfigSection.Loaded);
                this.dataModel              = new ServerDataModel(channel);
            }

            //enable the following line to enable a model with 'test' data
            //this.dataModel                  = new TestingDataModel();

            this.InitViews();
            this.InitDataConnectors();

            
            GuiHost.PushView(this.viewSwitcher);

            this.mouseEventDispatcher.GuiRoot = this.viewSwitcher;

            base.Initialize();
        }


        private void InitViews()
        {
            this.viewSwitcher           = new ViewSwitcher(this.GraphicsDevice);
        }

        private void InitDataConnectors()
        {
            //Engines
            //  left
            this.dataConnectors.Add(new MultiValueDataConnector(this.viewSwitcher.EnginesView.Engines.LeftEngine.TemperatureGauge, this.dataModel));
            this.dataConnectors.Add(new MultiValueDataConnector(this.viewSwitcher.EnginesView.Engines.LeftEngine.RpmGauge, this.dataModel));
            this.dataConnectors.Add(new DataConnector(this.viewSwitcher.EnginesView.Engines.LeftEngine.PressureGauge, this.dataModel));

            //  right
            this.dataConnectors.Add(new MultiValueDataConnector(this.viewSwitcher.EnginesView.Engines.RightEngine.TemperatureGauge, this.dataModel));
            this.dataConnectors.Add(new MultiValueDataConnector(this.viewSwitcher.EnginesView.Engines.RightEngine.RpmGauge, this.dataModel));
            this.dataConnectors.Add(new DataConnector(this.viewSwitcher.EnginesView.Engines.RightEngine.PressureGauge, this.dataModel));

            //HSI
            this.dataConnectors.Add(new MultiValueDataConnector(this.viewSwitcher.AdiHsiView.AdiHisPanel.HsiGauge, this.dataModel));
            this.dataConnectors.Add(new MultiValueDataConnector(this.viewSwitcher.AdiHsiView.AdiHisPanel.AdiGauge, this.dataModel));

            //Environment
            this.dataConnectors.Add(new MultiTwoWayDataConnector(this.viewSwitcher.EnvironemntOxygenTest, this.dataModel));

            //Testing Panel
            this.dataConnectors.Add(new MultiTwoWayDataConnector(this.viewSwitcher.TestingView.panel, this.dataModel));
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            this.dataModel.Update();
            this.dataConnectors.Update();
            this.mouseEventDispatcher.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black); //Color.CornflowerBlue

            // TODO: Add your drawing code here

            this.spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            GuiHost.Render(this.spriteBatch, this.GraphicsDevice, new Point(this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height));
            this.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
